/**
 * ElectronExchanger Server Code File
 * 
 * File:        thread_helper.c
 * Brief:       BRIEF
 * Author:      Chen Kangbing
 * Email:       ckblau@outlook.com
 * Modified:    2020/09/08 17:07:32
 *
 */


#include "thread_helper.h"

#include <errno.h>
#include <stdio.h>
#include <string.h>


int thread_start(pthread_t *thread_id, THREAD_BODY *thread_workbody,
                 void *thread_arg) {
    int rv = -1;
    pthread_attr_t thread_attr;

    printf("Starting thread...\n");

    if (pthread_attr_init(&thread_attr)) {
        printf("pthread_attr_init() failed: %s\n", strerror(errno));
        goto CleanUp;
    }

    if (pthread_attr_setstacksize(&thread_attr, 120 * 1024)) {
        printf("pthread_attr_setstacksize() failed: %s\n", strerror(errno));
        goto CleanUp;
    }

    if (pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED)) {
        printf("pthread_attr_setdetachstate() failed: %s\n", strerror(errno));
        goto CleanUp;
    }

    if (pthread_create(thread_id, &thread_attr, thread_workbody, thread_arg)) {
        printf("Failed to create thread: %s\n", strerror(errno));
        goto CleanUp;
    }

    rv = 0;
CleanUp:
    pthread_attr_destroy(&thread_attr);
    return rv;
}

